package com.yunweiweishi.xuhuanpaltform.yuhuan_app.controller;


import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.conf.CommonResult;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.pojo.Expert;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.service.ExpertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "专家", tags = {"专家controller"})
@RestController
@RequestMapping("expert")
public class ExpertController {


    @Autowired
    private ExpertService expertService;


    @Autowired
    private CommonResult result;



    /**
     *
     * 通过姓名查找专家
     * @param expertName
     * @return
     */
    @ApiOperation( value = "查询专家")
    @GetMapping("findExceptByExpertName")
    public CommonResult findExceptByExpertName(@RequestParam("expertName") String expertName) {

        try {

            Expert expert= expertService.findExceptByExpertName(expertName);
            result.setData(expert);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    /**
     *
     * 根据手机号进行查询专家
     */
    @ApiOperation( value = "根据手机号查询专家")
    @GetMapping("findExceptByPhoneNumber")
    public CommonResult findExpertByPhoneNumber(@RequestParam("phoneNumber") String phoneNumber) {

        try {

            Expert expert= expertService.findExpertByPhoneNumber(phoneNumber);
            result.setData(expert);
            result.setMsg("成功！");
            result.setState(200);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    @ApiOperation( value = "根据id进行查询")
    @GetMapping("/expertId")
    public CommonResult findExpertById(@RequestParam("id") Long id) {
        try {

            Expert expert= expertService.findExpertById(id);
            result.setData(expert);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }

    }


    /**
     * 添加专家
     * @param expert
     * @return
     */
    @ApiOperation( value = "添加专家")
    @PostMapping("addExpert")
    public CommonResult AddExpert(@RequestBody Expert expert) {
        try {

          expertService.AddExpert(expert);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }

    }
    @ApiOperation( value = "根据id进行专家的更新")
    @PutMapping("/updateExpert/{id}")
    public CommonResult UpdateExpertById(@RequestBody Expert expert) {

        try {

            expertService.UpdateExpertById(expert);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }

    }
    @ApiOperation( value = "根据id删除专家")
    @DeleteMapping("deleteById")
    public CommonResult deleteExpertById(@RequestParam("id") Long id) {

        try {

            expertService.deleteExpertById(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }
}
